'use strict'
function calcRateArray(p, rateArray) {
 let rate = rateArray.slice()
 let t = rate[0] + Math.floor(rate[0] / 7) * 4 + rate[1]
 if (p > t) {
   p = t
 }
 let p1 = p
 let p2 = 0

 if (p > rate[0]) {
   p1 = rate[0]
   p2 = p - rate[0]
 }

 if (!rate[2]) {
   rate[0] -= p1
   rate[1] += p1
 } else if (!rate[3]) {
   rate[0] -= p1
   let x = Math.floor(p1 / 3)
   let y = p1 % 3
   rate[1] += 2 * x + y
   rate[2] += x
   if (p2) {
     rate[1] -= p2
     rate[2] += p2
   }
 } else if (rate[3]) {
   rate[0] -= p1
   let x = Math.floor(p1 / 7)
   let y = p1 % 7
   if (y <= 4) {
     rate[1] += 4 * x + y
     rate[2] += 2 * x
   } else {
     rate[1] += 4 * x + 4
     rate[2] += 2 * x + y - 4
   }
   rate[3] += x
   if (p2) {
     rate[1] -= p2
     x = Math.floor(p2 / 3)
     y = p2 % 3
     rate[2] += 2 * x + y
     rate[3] += x
   }
 }
 return rate
}

function calcLuckyRate(lucky, luckyRate) {
 let min = luckyRate[0]
 let max = luckyRate[1]
 let ratio = luckyRate[2]
 if (lucky <= min) {
   return lucky
 }
 if (lucky > min && lucky <= max) {
   return (lucky - min) * ratio
 }
 if (lucky > max) {
   return max*ratio + lucky - max
 }
}

function getIndexByRate(rateArray) {
 let random = Math.floor(Math.random() * 100)
 if (random < rateArray[0]) {
   return 0
 }
 random -= rateArray[0]
 if (random < rateArray[1]) {
   return 1
 }
 random -= rateArray[1]
 if (random < rateArray[2]) {
   return 2
 }
 return 3
}

let lucky = calcLuckyRate(200,[0, 25, 2])
console.log(lucky)
let rateArray = calcRateArray(25,[99, 1, 1, 1])
console.log(rateArray)
for(var i = 0;i<1;i++){
  console.log(getIndexByRate(rateArray))
}
